﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.Web.ModelService;
using Newtonsoft.Json;
using Fly.Common.Linq;
using Fly.Common.WorkFlow;
using Telerik.Web.UI;
using Telerik.Web.UI.Skins;

namespace RN.ERP.Web.Person
{
    public partial class PersonExpertSelect : BasePage
    {
        protected string AreaCode
        {
            get
            {
                return UserInfo.CurrentArea;
            }
        }


        private PersonService.PersonServiceClient personServiceClient =new PersonService.PersonServiceClient();
        private BasicService.BasicServiceClient basicServiceClient = new BasicService.BasicServiceClient();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetParams();
            }
        }

        private void GetParams()
        {
            hdnClassID.Value = Request["classId"];
            Guid empGuid = Guid.Empty;
            Guid gid = Guid.Empty;
            if (!string.IsNullOrEmpty(Request.QueryString["empGuid"]))
            {
                if (Guid.TryParse(Request.QueryString["empGuid"], out empGuid))
                {
                    hdEmpGuid.Value = empGuid.ToString();
                }
            }
            if (!string.IsNullOrEmpty(Request.QueryString["gid"]))
            {
                if (Guid.TryParse(Request.QueryString["gid"], out gid))
                {
                    hdGid.Value = gid.ToString();
                    hdStatus.Value = "1";
                }
            }
            if (!string.IsNullOrEmpty(Request.QueryString["Flag"]))
            {
                hdFlag.Value = Request.QueryString["Flag"];
            }

        }

        /// <summary>
        /// 客户端grid刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            if (e.Argument == "Rebind")
            {
                ListCorps.MasterTableView.SortExpressions.Clear();
                ListCorps.MasterTableView.GroupByExpressions.Clear();
                ListCorps.Rebind();
            }
        }

        protected void ListCorps_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            string corpName = txtCorpName.Text.Trim();
            if (!string.IsNullOrEmpty(hdGid.Value) || !string.IsNullOrEmpty(corpName))
            {
                string ClassId = Request.QueryString["classId"];
                var queryCondition = GetCondition();
                int cnt = 0;
                var corp = personServiceClient.GetExpertsList(ListCorps.CurrentPageIndex + 1, ListCorps.PageSize, queryCondition, UserInfo.CurrentArea, out cnt);
                var empInfo = JsonConvert.DeserializeObject<IList<tb_Emp_Manager>>(corp);
                ListCorps.AllowCustomPaging = true;
                ListCorps.VirtualItemCount = cnt;
                ListCorps.DataSource = empInfo;
            }
        }

        //protected string GetCorpClassKind(string type)
        //{
        //    string output = string.Empty;
        //    if (!String.IsNullOrEmpty(type))
        //    {
        //        Dictionary<string, string> CorpTypeDic = JsonConvert.DeserializeObject<Dictionary<string, string>>
        //            (Application[ApplicationConstant.APPLICATION_CORPUSERTYPE].ToString());
        //        try
        //        {
        //            output = CorpTypeDic[type];
        //        }
        //        catch
        //        {
        //            output = "";
        //        }
        //    }
        //    return output;
        //}

        private string GetCondition()
        {
            List<QueryPersonInfoCondition> queryPersonInfoList = new List<QueryPersonInfoCondition>();
            QueryPersonInfoCondition queryPersonInfoCondition = new QueryPersonInfoCondition();
            string classId = Request.QueryString["classId"];
            string corpName = txtCorpName.Text.Trim();
            int clsId = -1;
            if (!string.IsNullOrEmpty(classId) && int.TryParse(classId, out clsId))
            {
                queryPersonInfoCondition.ClassId = new ParameterValue<int?>() { O = OptEnum.Equal, V = clsId };
            }
            if (!string.IsNullOrEmpty(corpName))
            {
                queryPersonInfoCondition.Name = new ParameterValue<string>() { O = OptEnum.Equal, V = corpName };
            }
            if (!string.IsNullOrEmpty(hdEmpGuid.Value))
            {
                Guid empGuid=Guid.Empty;
                if(Guid.TryParse(hdEmpGuid.Value,out empGuid))
                {
                    queryPersonInfoCondition.EmpGuid = new ParameterValue<Guid>() { O = OptEnum.NotEqual, V = empGuid };
                }
            }
            queryPersonInfoList.Add(queryPersonInfoCondition);
            return JsonConvert.SerializeObject(queryPersonInfoList);
        }

        public string ProcessIDNum(string inputString)
        {
            try
            {
                string ecryptedString = string.Empty;
                if (!string.IsNullOrEmpty(inputString) && inputString.Length > 3)
                {
                    ecryptedString = inputString.Substring(0, 3);
                    for (int i = 0; i < inputString.Length - 4; i++)
                    {
                        ecryptedString += "*";
                    }
                    return ecryptedString + inputString.Substring(inputString.Length - 4, 3);
                }
                return "";
            }
            catch {  return "";
            }
           
        }

        public override bool IsCheckPermission
        {
            get { return false; }
        }
    }
}
